package org.dromara.web.controller.app;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.hutool.core.util.ObjectUtil;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.service.OssService;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.dao.system.domain.vo.SysOssUploadVo;
import org.dromara.dao.system.domain.vo.SysOssVo;
import org.dromara.web.plugins.file.LocalFileResolver;
import org.dromara.web.service.system.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 *  上传相关
 *
 */
@RestController
@RequestMapping("/upload")
public class UploadController {

    @Autowired
    ISysOssService ossService;
    @Autowired
    LocalFileResolver localFileResolver;

    /**
     * 上传文件
     *
     * @param file 文件
     */
    @SaIgnore
    @PostMapping(value = "/standard", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<SysOssUploadVo> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            return R.fail("上传文件不能为空");
        }
        SysOssVo oss = ossService.uploadOnly(file);
        SysOssUploadVo uploadVo = new SysOssUploadVo();
        uploadVo.setUrl(oss.getUrl());
        uploadVo.setFileName(oss.getOriginalName());
        return R.ok(uploadVo);
    }


    /**
     * 上传文件
     *
     * @param file 文件
     */
    @SaIgnore
    @PostMapping(value = "/chat", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<String> uploadChatFile(@RequestPart("file") MultipartFile file, String fileName) {

        return R.ok("", localFileResolver.uploadFile(file, fileName));
    }



}
